const url = require('node:url');
const getAllUsers = require('../../handlers/getAllUsers');
const createUser = require('../../handlers/createUser');
const deleteUser = require('../../handlers/deleteUser');
const getUser = require('../../handlers/getUser');
const updateUser = require('../../handlers/updateUser');

const usersRoutes = (req, res) => {
  const parseUrl = url.parse(req.url, true);
  const method = req.method;
  const path = parseUrl.pathname;

  res.setHeader('Content-type', 'application/json');

  if (path === '/users' && method === 'GET') {
    getAllUsers(req, res);
  } else if (path === '/users' && method === 'POST') {
    createUser(req, res);
  } else if (path.startsWith('/users/') && method === 'GET') {
    getUser(req, res);
  } else if (path.startsWith('/users/') && method === 'PUT') {
    updateUser(req, res);
  } else if (path.startsWith('/users/') && method === 'DELETE') {
    deleteUser(req, res);
  } else {
    res.writeHead(404);
    res.end(JSON.stringify({ message: 'Route not found' }));
  }
};

module.exports = usersRoutes;